package com.amigo.online.provider.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.amigo.online.common.base.service.BaseRedisService;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.provider.user.common.RwRedisService;
import com.amigo.online.provider.user.service.RedisService;

@Service
public class RedisServiceImpl extends BaseRedisService implements RedisService {

    @Autowired
    RwRedisService rwRedisService;

    @Override
    public List<Object> listFromRedis(Integer page, Integer size, String key1, String key2, Response r) {
        r.setData(0);//默认0查询redis成功
        Long start = 0L;
        Long end = -1L;
        boolean checkNum = false;
        if (page != null && size != null) {
            Integer i_offset = (page - 1) * size;
            start = (long) (i_offset);
            end = (long) (i_offset + size - 1);
            checkNum = true;
        }
        List<Object> list1 = rwRedisService.lrange(key1, start, end);
        if (checkNum) {
            if (list1 == null || list1.size() != size) {
                r.setData(1);
                return null;
            } else {
                List<Object> list2 = rwRedisService.opsForHashRead().multiGet(key2, list1);
                for (Object o : list2) {
                    if (o == null) {
                        r.setData(2);
                        return list1;
                    }
                }
                return list2;
            }
        } else {
            if (list1 == null || list1.size() == 0) {
                r.setData(1);
                return null;
            } else {
                List<Object> list2 = rwRedisService.opsForHashRead().multiGet(key2, list1);
                if (list2.size() == 0) {
                    r.setData(2);
                    return list1;
                }
                return list2;
            }
        }


    }

    @Override
    public void deleteKey(String key) {
        rwRedisService.delete(key);
    }
}
